//
//  TYTheme.m
//  TuyaSmartPublic
//
//  Created by 高森 on 2017/3/9.
//  Copyright © 2017年 Tuya. All rights reserved.
//

#if TARGET_OS_IOS

#import "TYThemeImpl.h"
#import <YYModel/YYModel.h>
#import "UIColor+TYTheme.h"
#import "TYCustomConfig.h"

//#if __has_include(<TYNavigationController/TYNavigationBarAppearance.h>)
//#import <TYNavigationController/TYNavigationBarAppearance.h>
//#elif __has_include("TYNavigationBarAppearance.h")
//#import "TYNavigationBarAppearance.h"
//#endif


@interface TYTheme () <YYModel>

// topBar
@property (nonatomic, strong) UIColor *status_bg_color;
@property (nonatomic, strong) UIColor *status_font_color;
@property (nonatomic, strong) UIColor *status_disable_color;
@property (nonatomic, strong) UIColor *status_select_color;
@property (nonatomic, strong) UIColor *navbar_font_color;



// tabBar
@property (nonatomic, strong) UIColor *tab_selected_color;
@property (nonatomic, strong) UIColor *tab_no_selected_color;
@property (nonatomic, strong) UIColor *tab_bg_color;

// background
@property (nonatomic, strong) UIColor *app_bg_color;

// list
@property (nonatomic, strong) UIColor *list_line_color;
@property (nonatomic, strong) UIColor *list_bg_color;
@property (nonatomic, strong) UIColor *list_sub_color;
@property (nonatomic, strong) UIColor *list_secondary_color;
@property (nonatomic, strong) UIColor *list_primary_color;
@property (nonatomic, strong) UIColor *list_cell_bg_color;

//card
@property (nonatomic, strong) UIColor *card_bg_color;
//alert
@property (nonatomic, strong) UIColor *alert_bg_color;
//button
@property (nonatomic, strong) UIColor *primary_button_bg_color;
@property (nonatomic, strong) UIColor *primary_button_font_color;
@property (nonatomic, strong) UIColor *primary_button_select_color;
@property (nonatomic, strong) UIColor *secondary_button_font_color;
@property (nonatomic, strong) UIColor *secondary_button_bg_color;
@property (nonatomic, strong) UIColor *secondary_button_select_color;

//源颜色
@property (nonatomic,strong) UIColor *B1;

@property (nonatomic,strong) UIColor *B2;

@property (nonatomic,strong) UIColor *B3;

@property (nonatomic,strong) UIColor *B4;

@property (nonatomic,strong) UIColor *B5;

@property (nonatomic,strong) UIColor *B6;

@property (nonatomic,strong) UIColor *B1_2;

@property (nonatomic,strong) UIColor *B2_2;

@property (nonatomic,strong) UIColor *B3_2;

@property (nonatomic,strong) UIColor *B4_2;

@property (nonatomic,strong) UIColor *B5_2;

@property (nonatomic,strong) UIColor *B6_2;

@property (nonatomic,strong) UIColor *M1;

@property (nonatomic,strong) UIColor *M2;

@property (nonatomic,strong) UIColor *M3;

@property (nonatomic,strong) UIColor *M4;

@property (nonatomic,strong) UIColor *M5;

@property (nonatomic,strong) UIColor *M1_1;

@property (nonatomic,strong) UIColor *M2_1;

@property (nonatomic,strong) UIColor *M3_1;

@property (nonatomic,strong) UIColor *M4_1;

@property (nonatomic,strong) UIColor *M1_2;

@property (nonatomic,strong) UIColor *M2_2;

@property (nonatomic,strong) UIColor *M3_2;

@property (nonatomic,strong) UIColor *M4_2;

@property (nonatomic,assign) CGFloat N1;

@property (nonatomic,assign) CGFloat N2;

@property (nonatomic,assign) CGFloat N3;

@property (nonatomic,assign) CGFloat N4;

@property (nonatomic,assign) CGFloat N5;

@property (nonatomic,assign) CGFloat N6;

@property (nonatomic,assign) CGFloat N7;

@property (nonatomic,assign) CGFloat N8;

@property (nonatomic,assign) CGFloat C1_1;

@property (nonatomic,assign) CGFloat C1_2;

@property (nonatomic,assign) CGFloat C1_3;

@property (nonatomic,assign) CGFloat C2;

@property (nonatomic,assign) CGFloat C3_1;

@property (nonatomic,assign) CGFloat C3_2;

@property (nonatomic,assign) CGFloat C3_3;

@property (nonatomic,assign) CGFloat C3_4;

@property (nonatomic,strong) NSString *appFont;

@property (nonatomic,strong) NSString *appBoldFont;

@end

@implementation TYTheme

static TYTheme *_theme = nil;
+ (TYTheme *)theme {
    if (!_theme) {
        _theme = [[TYTheme alloc]init];
        NSDictionary *dict = [self readThemeData];
        if (!dict) {
            NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customColor" ofType:@"plist"];
            dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        }
        
        NSDictionary *colors = [dict valueForKey:[TYTheme colorsKey]];
        
        _theme.dynamicClolr = [_theme getDynamicClolr];
        
        NSString *B1 = @"B1";
        NSString *colorB1 = colors[B1] ? colors[B1] : @"#F8F8F8";
        _theme.B1 = [UIColor ty_themeColorWithHexString:colorB1];
        
        NSString *B2 = @"B2";
        NSString *colorB2 = colors[B2] ? colors[B2] : @"#FFFFFF";
        _theme.B2 = [UIColor ty_themeColorWithHexString:colorB2];
        
        NSString *B3 = @"B3";
        NSString *colorB3 = colors[B3] ? colors[B3] : @"#FFFFFF";
        _theme.B3 = [UIColor ty_themeColorWithHexString:colorB3];
        
        NSString *B4 = @"B4";
        NSString *colorB4 = colors[B4] ? colors[B4] : @"#FFFFFF";
        _theme.B4 = [UIColor ty_themeColorWithHexString:colorB4];
        
        NSString *B5 = @"B5";
        NSString *colorB5 = colors[B5] ? colors[B5] : @"#FFFFFF";
        _theme.B5 = [UIColor ty_themeColorWithHexString:colorB5];
        
        _theme.B6 = _theme.B1.ty_isDarkcolor.boolValue ?
        [_theme.B1 ty_colorWithForwordColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.1]] :
        [UIColor ty_themeColorWithHexString:@"0xFFFFFF"];
        _theme.B1_2 = _theme.B1.ty_highlyColor;
        _theme.B2_2 = _theme.B2.ty_highlyColor;
        _theme.B3_2 = _theme.B3.ty_highlyColor;
        _theme.B4_2 = _theme.B4.ty_highlyColor;
        _theme.B5_2 = _theme.B5.ty_highlyColor;
        _theme.B6_2 = _theme.B6.ty_highlyColor;
        
        NSString *M1 = @"M1";
        NSString *colorM1 = colors[M1] ? colors[M1] : @"#FF5A28";
        _theme.M1 = [UIColor ty_themeColorWithHexString:colorM1];
        
        NSString *M2 = @"M2";
        NSString *colorM2 = colors[M2] ? colors[M2] : @"#FF4444";
        _theme.M2 = [UIColor ty_themeColorWithHexString:colorM2];
        
        NSString *M3 = @"M3";
        NSString *colorM3 = colors[M3] ? colors[M3] : @"#00CC99";
        _theme.M3 = [UIColor ty_themeColorWithHexString:colorM3];
        
        NSString *M4 = @"M4";
        NSString *colorM4 = colors[M4] ? colors[M4] : @"#1989FA";
        _theme.M4 = [UIColor ty_themeColorWithHexString:colorM4];
        
        NSString *M5 = @"M5";
        NSString *colorM5 = colors[M5] ? colors[M5] : @"#FF5A28";
        _theme.M5 = [UIColor ty_themeColorWithHexString:colorM5];
        
        _theme.M1_1 = [UIColor ty_themeColorWithHexString:colorM1 alpha:0.2];
        _theme.M2_1 = [UIColor ty_themeColorWithHexString:colorM2 alpha:0.2];
        _theme.M3_1 = [UIColor ty_themeColorWithHexString:colorM3 alpha:0.2];
        _theme.M4_1 = [UIColor ty_themeColorWithHexString:colorM4 alpha:0.2];
        _theme.M1_2 = _theme.M1.ty_highlyColor;
        _theme.M2_2 = _theme.M2.ty_highlyColor;
        _theme.M3_2 = _theme.M3.ty_highlyColor;
        _theme.M4_2 = _theme.M4.ty_highlyColor;
        
        NSDictionary *colorAlpha = [dict valueForKey:[TYTheme colorAlphaKey]];
        
        NSString *N1 = @"N1";
        NSNumber *N1Alpha = colorAlpha[N1] ? colorAlpha[N1] : @(0.9);
        _theme.N1 = [N1Alpha floatValue];
        
        NSString *N2 = @"N2";
        NSNumber *N2Alpha = colorAlpha[N2] ? colorAlpha[N2] : @(0.7);
        _theme.N2 = [N2Alpha floatValue];
        
        NSString *N3 = @"N3";
        NSNumber *N3Alpha = colorAlpha[N3] ? colorAlpha[N3] : @(0.5);
        _theme.N3 = [N3Alpha floatValue];
        
        NSString *N4 = @"N4";
        NSNumber *N4Alpha = colorAlpha[N4] ? colorAlpha[N4] : @(0.3);
        _theme.N4 = [N4Alpha floatValue];
        
        NSString *N5 = @"N5";
        NSNumber *N5Alpha = colorAlpha[N5] ? colorAlpha[N5] : @(0.7);
        _theme.N5 = [N5Alpha floatValue];
        
        NSString *N6 = @"N6";
        NSNumber *N6Alpha = colorAlpha[N6] ? colorAlpha[N6] : @(0.2);
        _theme.N6 = [N6Alpha floatValue];
        
        NSString *N7 = @"N7";
        NSNumber *N7Alpha = colorAlpha[N7] ? colorAlpha[N7] : @(0.1);
        _theme.N7 = [N7Alpha floatValue];
        
        NSString *N8 = @"N8";
        NSNumber *N8Alpha = colorAlpha[N8] ? colorAlpha[N8] : @(0.4);
        _theme.N8 = [N8Alpha floatValue];
        
        NSDictionary *font = [dict valueForKey:[TYTheme fontKey]];
        NSString *fontKey = @"app_font";
        NSString *appFont = font[fontKey];
        _theme.appFont = [self appFontNameFromFileName:appFont];
        
        NSString *boldFontKey = @"app_bold_font";
        NSString *boldFont = font[boldFontKey];
        _theme.appBoldFont = [self appFontNameFromFileName:boldFont];
        if (!_theme.appBoldFont || _theme.appBoldFont.length == 0) {
            _theme.appBoldFont = _theme.appFont;
        }
        
        NSDictionary *corner = [dict valueForKey:[TYTheme cornerKey]];
        //straight代表直角 angle代表圆角
        NSString *straightCorner = @"straight";
        NSString *angleCorner = @"angle";
        NSString *alertCornerKey = @"alert_corner_type";
        NSString *cardCornerKey = @"card_corner_type";
        NSString *buttonCornerKey = @"button_corner_type";
        
        NSString *alertCorner = corner[alertCornerKey] ? corner[alertCornerKey] : angleCorner;
        NSString *cardCorner = corner[cardCornerKey] ? corner[cardCornerKey] : angleCorner;
        NSString *buttonCorner = corner[buttonCornerKey] ? corner[buttonCornerKey] : angleCorner;
        
        _theme.C1_1 = ([buttonCorner isEqualToString:straightCorner]) ? 8 : 24;
        _theme.C1_2 = ([buttonCorner isEqualToString:straightCorner]) ? 8 : 20;
        _theme.C1_3 = ([buttonCorner isEqualToString:straightCorner]) ? 8 : 15;
        _theme.C2 = ([alertCorner isEqualToString:straightCorner]) ? 4 : 16;
        _theme.C3_1 = ([cardCorner isEqualToString:straightCorner]) ? 4 : 16;
        _theme.C3_2 = ([cardCorner isEqualToString:straightCorner]) ? 4 : 12;
        _theme.C3_3 = ([cardCorner isEqualToString:straightCorner]) ? 4 : 8;
        _theme.C3_4 = ([cardCorner isEqualToString:straightCorner]) ? 4 : 4;
        
        _theme.app_bg_color = _theme.B1;
        _theme.status_bg_color = _theme.B2;
        _theme.status_font_color = _theme.B2.N5;
        _theme.status_select_color = [_theme.status_font_color ty_highlyColor];
        _theme.status_disable_color = [_theme.status_font_color colorWithAlphaComponent:0.2];
        _theme.navbar_font_color = _theme.status_font_color;
        
        _theme.card_bg_color = _theme.B3;
        _theme.alert_bg_color = _theme.B4;
        
        _theme.tab_no_selected_color = _theme.B5.N3;
        _theme.tab_bg_color = _theme.B5;
        _theme.tab_selected_color = _theme.M5;
        
        _theme.list_bg_color = _theme.app_bg_color;
        _theme.list_cell_bg_color = _theme.B6;
        _theme.list_sub_color = _theme.B6.N2;
        _theme.list_secondary_color = _theme.B6_2.N1;
        _theme.list_primary_color = _theme.B6.N1;
        _theme.list_line_color = _theme.list_line_color.N7;
        
        _theme.primary_button_bg_color = _theme.M1;
        _theme.primary_button_font_color = _theme.M1.N1;
        _theme.primary_button_select_color = _theme.M1_2;
        _theme.secondary_button_bg_color = [UIColor clearColor];
        _theme.secondary_button_font_color = _theme.M1;
        _theme.secondary_button_select_color = [_theme.B1 ty_highlyColor];
    }
    return _theme;
}

+ (void)reset {
    _theme = nil;
}

+ (NSDictionary *)readThemeData
{
    return [TYCustomConfig sharedInstance].themeConfigurationDic;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIColor *color = self.status_font_color;
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    CGFloat light = components[0] * 0.3 + components[1] * 0.59 + components[2] * 0.11;
    if (light < 0.5) {
        return UIStatusBarStyleDefault;
    } else {
        return UIStatusBarStyleLightContent;
    }
}

+ (NSString *)colorsKey {
    return @"colors";
}

+ (NSString *)fontKey {
    return @"font";
}

+ (NSString *)colorAlphaKey {
    return @"color_alpha";
}

+ (NSString *)cornerKey {
    return @"corner";
}

+ (NSString *)appFontNameFromFileName:(NSString *)fileName
{
    NSURL *url = [NSBundle mainBundle].bundleURL;
    if ([fileName containsString:@".ttf"]) {
        url = [url URLByAppendingPathComponent:fileName];
    } else {
        url = [url URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.ttf",fileName]];
    }
    NSData *fontData = [NSData dataWithContentsOfURL:url];
    if (fontData) {
        CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((CFDataRef)fontData);
        CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
        if (!loadedFont) {
            CGDataProviderRelease(fontDataProvider);
            CGFontRelease(loadedFont);
            return @"";
        }
        CGDataProviderRelease(fontDataProvider);
        NSString *postScriptName = (NSString *)CFBridgingRelease(CGFontCopyPostScriptName(loadedFont));
        CGFontRelease(loadedFont);
        if (postScriptName) {
            return postScriptName;
        }
    }
    return @"";
}

- (BOOL)isDarkMode {
    return self.app_bg_color.ty_isDarkcolor.boolValue;
}

- (BOOL)isLightMode {
    return !(self.app_bg_color.ty_isDarkcolor.boolValue);
}

- (UIColor *)getDynamicClolr {
    NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"dynamicClolr"];
    UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
    return color?color:[UIColor ty_themeColorWithHexString:@"#F59600"];
}

- (void)cacheDynamicColor:(UIColor *)dynamicClolr {
    if (dynamicClolr) {
        NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:dynamicClolr];
        [[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"dynamicClolr"];
    }
}

@end

//@implementation TYTheme (Appearance)
//
//+ (void)load {
//    if (TYTheme.theme != nil) {
//        [self initAppearance];
//    }
//}
//
//+ (void)initAppearance {
//    [self initNavigationBar];
//    [self initTabBar];
//    [self initWebView];
//}
//
//+ (void)initNavigationBar {
//    UINavigationBar *navigationBar = [UINavigationBar appearance];
//    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [TYTheme theme].status_font_color};
////    navigationBar.tintColor = [TYTheme theme].status_font_color;
//    //    navigationBar.barTintColor = [TYTheme theme].status_bg_color;
//    //    navigationBar.translucent = NO;
//
////#if __has_include(<TYNavigationController/TYNavigationBarAppearance.h>) || __has_include("TYNavigationBarAppearance.h")
////    [TYNavigationBarAppearance appearance].statusBarStyle = [TYTheme theme].preferredStatusBarStyle;
////#endif
//
//}
//
//+ (void)initTabBar {
//    UITabBar *tabBar = [UITabBar appearance];
//    tabBar.tintColor = [TYTheme theme].navbar_font_color;
//    tabBar.barTintColor = [UIColor whiteColor];
//    UIColor *tabbarSelectColor = [TYTheme theme].tab_selected_color;
//    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: TY_HexColor(0x495054)} forState:UIControlStateNormal];
//
//    if (tabbarSelectColor != nil) {
//        NSDictionary *selectAttr = @{
//                                     NSForegroundColorAttributeName: tabbarSelectColor
//                                     };
//        [[UITabBarItem appearance] setTitleTextAttributes:selectAttr forState:UIControlStateSelected];
//    }
//}
//
//+ (void)initWebView {
//    // UIWebView *webView = [UIWebView appearance];
//    // webView.backgroundColor = [TYTheme theme].app_bg_color;
//
//    WKWebView *wkWebView = [WKWebView appearance];
//    wkWebView.backgroundColor = [TYTheme theme].app_bg_color;
//}
//
//
////// http://stackoverflow.com/questions/17070582/using-uiappearance-and-switching-themes
////+ (void)reloadAppearance {
////    [self initAppearance];
////
////    NSArray *windows = [UIApplication sharedApplication].windows;
////    for (UIWindow *window in windows) {
////        for (UIView *subView in window.subviews) {
////            [subView removeFromSuperview];
////            [window addSubview:subView];
////        }
////        [window.rootViewController setNeedsStatusBarAppearanceUpdate];
////    }
////}
//
//@end

#endif
